import {
  CallHandler,
  ExecutionContext,
  Injectable,
  Logger,
  NestInterceptor,
} from '@nestjs/common'
import { Observable, tap } from 'rxjs'

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  private logger = new Logger(LoggingInterceptor.name, { timestamp: false })

  intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<any> {
    const request = context.switchToHttp().getRequest()
    const method = request.method
    const url = request.url
    const now = Date.now()


    this.logger.debug(`请求开始: ${method} ${url}`)

    return next
      .handle()
      .pipe(
        tap(() => {
          this.logger.debug(`请求结束: ${method} ${url} - 耗时: ${Date.now() - now}ms`)
        }),
      )
  }
}
